package com.fly.swt.window;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * Shell 切换，更优方案参见 StackLayoutSample
 * 
 * @author 00fly
 * @version [版本号, 2020年5月6日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class G
{
    public static void main(String[] args)
    {
        Display d = Display.getDefault();
        Shell s = new Shell(d);
        new T(s, SWT.NONE);
        s.setLayout(new FillLayout());
        s.open();
        s.pack();
        while (!s.isDisposed())
        {
            if (!d.readAndDispatch())
            {
                d.sleep();
            }
        }
        d.dispose();
    }
}

class T extends Composite
{
    public T(Composite p, int style)
    {
        super(p, style);
        this.setLayout(new FillLayout());
        final Button b = new Button(this, SWT.NONE);
        b.setText("First");
        b.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                Shell s = b.getShell();
                new R(s, SWT.NONE);
                b.getParent().dispose();
                s.layout();
            }
        });
    }
}

class R extends Composite
{
    public R(Composite p, int style)
    {
        super(p, style);
        this.setLayout(new FillLayout());
        final Button b = new Button(this, SWT.NONE);
        b.setText("Second");
        b.addSelectionListener(new SelectionAdapter()
        {
            @Override
            public void widgetSelected(SelectionEvent e)
            {
                Shell s = b.getShell();
                new T(s, SWT.NONE);
                b.getParent().dispose();
                s.layout();
            }
        });
    }
}